package com.dragon.english_practice_back.util;


import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class PasswordUtil {
    private static DelegatingPasswordEncoder delegatingPasswordEncoder;
    @PostConstruct
    public void init(){
        delegatingPasswordEncoder = (DelegatingPasswordEncoder) PasswordEncoderFactories.createDelegatingPasswordEncoder();
        delegatingPasswordEncoder.setDefaultPasswordEncoderForMatches(new BCryptPasswordEncoder());
        log.info("PasswordUtil init");
    }

    //生成加密密码
    public static String encode(String rawPassword){
        return delegatingPasswordEncoder.encode(rawPassword);
    }

    //对原密和加密进行匹配
    public static boolean matches(String rawPassword, String prefixEncodedPassword){
        return delegatingPasswordEncoder.matches(rawPassword, prefixEncodedPassword);
    }


}

